
Imports System.Runtime.Remoting

<Serializable()> Public Class Subscription
    Inherits MarshalByRefObject

    ' We should keep a copy of the U.R.I. used to subscribe, just in case
    ' the client code would like to make some sort of list.
    Public URI As String

    ' We need a publication object that will send us its issue events.
    Public WithEvents Publication As Publication

    ' For some interesting reason, remoting needs an instance of our local
    ' subscriber object to maintain communications.
    Public Subscriber As Subscriber

    Public Sub New(ByVal URI As String, ByRef Subscriber As Subscriber)
        Me.URI = URI
        Me.Subscriber = Subscriber

        ' Since a U.R.I. can exist without an actual publisher on the other
        ' end, our request could throw an exception, which we catch below.
        Try
            ' Connect to the remote publication using the U.R.I. provided.
            Me.Publication = RemotingServices.Connect(GetType(Publication), Me.URI)
        Catch ex As Exception
            MsgBox(ex.ToString)
            ' For now, we are just keeping the exception to ourselves and
            ' not letting the user know what happened.  In the future, we
            ' may want to pass the exception along back up to the client
            ' code and, therefore, the user.
        End Try
    End Sub

    ' This event set passes an issue event from a publication onto any
    ' subscriber object that may be handling our events.
    Public Event ReceiveIssue(ByVal Publication As Publication, ByVal Issue As Object)
    Public Sub PublishedIssue(ByVal Issue As Object) Handles Publication.PublishedIssue
        RaiseEvent ReceiveIssue(Me.Publication, Issue)
    End Sub
End Class
